package wpwork.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wpwork.base.ActionResult;
import wpwork.base.Pagination;
import wpwork.base.UserInfo;
import wpwork.base.controller.SuperController;
import wpwork.base.vo.PageListVO;
import wpwork.base.vo.PaginationVO;
import wpwork.entity.WorkResourcesEntity;
import wpwork.entity.WorkRsattendanceEntity;
import wpwork.entity.WorkRsattendanceEntity;
import wpwork.permission.entity.UserEntity;
import wpwork.service.WorkResourcesService;
import wpwork.service.WorkRsattendanceService;
import wpwork.util.*;

import javax.validation.Valid;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Contract
 * 版本： V3.0.0
 * 版权： 武汉维普科技有限公司(https://www.websharp.cn)
 * 作者： wpwork开发平台组
 * 日期： 2020-12-31
 * 考勤配置
 */
@RestController
@Tag(name = "示例接口", description = "WorkRsattendance")
@RequestMapping("/api/example/WorkRsattendance")
public class WorkRsattendanceController extends SuperController<WorkRsattendanceService, WorkRsattendanceEntity> {

    @Autowired
    private WorkRsattendanceService workRsattendanceService;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private WorkResourcesService workResourcesService;
    @Autowired
    private WorkPersonJobController workPersonJobController;

    @PostMapping("/getOne")
    public ActionResult<WorkRsattendanceEntity> getOne(@RequestParam String id) {
        WorkRsattendanceEntity entity = workRsattendanceService.getById(id);
        return ActionResult.success(entity);
    }

    @PostMapping("/add")
    public ActionResult<WorkRsattendanceEntity> add(@RequestBody @Valid WorkRsattendanceEntity workRsattendance) {
        WorkRsattendanceEntity entity = new WorkRsattendanceEntity();
        entity.setId(UUID.randomUUID().toString());
        entity.setEnabledMark(workRsattendance.getEnabledMark());
        entity.setType(workRsattendance.getType());
        entity.setAutoattendance(workRsattendance.getAutoattendance());
        entity.setDistance(workRsattendance.getDistance() == null ? 0 : workRsattendance.getDistance());
        entity.setMapupload(workRsattendance.getMapupload());
        entity.setTime(workRsattendance.getTime());
        entity.setTerminal(workRsattendance.getTerminal());
        entity.setResourcesId(workRsattendance.getResourcesId());
        entity.setCreatorTime(new Date());
        String jwtToken = ServletUtil.getRequest().getHeader("Authorization");
        UserInfo userInfo = userProvider.get(jwtToken);
        entity.setLastModifyUserId(userInfo.getUserId());

        boolean save = workRsattendanceService.save(entity);
        WorkResourcesEntity entity2 = new WorkResourcesEntity();
        entity2.setId(workRsattendance.getResourcesId());
        entity2.setAttendanceId(entity.getId());
        entity2.setAttendance(entity.getEnabledMark());
        workResourcesService.updateById(entity2);
        if(entity.getEnabledMark() == 1
                && !"".equals(entity.getTime()) && !"".equals(entity.getTerminal())
                && entity.getTime() != null && entity.getTerminal() != null
                && !entity.getTime().trim().matches("^\"?\\s*\"?$")
                && !entity.getTerminal().trim().matches("^\"?\\s*\"?$")){
            workPersonJobController.addAttendanceJob(entity, 0);
        }
        if(save){
            return ActionResult.success("新增成功");
        }else{
            return ActionResult.fail("新增失败");
        }
    }

    @PostMapping("/update")
    public ActionResult<WorkRsattendanceEntity> update(@RequestBody @Valid WorkRsattendanceEntity workRsattendance) {
        WorkRsattendanceEntity entity = new WorkRsattendanceEntity();
        entity.setId(workRsattendance.getId());
        entity.setEnabledMark(workRsattendance.getEnabledMark());
        entity.setType(workRsattendance.getType());
        entity.setAutoattendance(workRsattendance.getAutoattendance());
        entity.setDistance(workRsattendance.getDistance());
        entity.setMapupload(workRsattendance.getMapupload());
        entity.setTime(workRsattendance.getTime());
        entity.setTerminal(workRsattendance.getTerminal());
        entity.setResourcesId(workRsattendance.getResourcesId());

        entity.setLastModifyTime(new Date());
        String jwtToken = ServletUtil.getRequest().getHeader("Authorization");
        UserInfo userInfo = userProvider.get(jwtToken);
        entity.setLastModifyUserId(userInfo.getUserId());
        boolean save = workRsattendanceService.updateById(entity);
        WorkResourcesEntity entity2 = new WorkResourcesEntity();
        entity2.setId(workRsattendance.getResourcesId());
        entity2.setAttendanceId(entity.getId());
        entity2.setAttendance(entity.getEnabledMark());
        workResourcesService.updateById(entity2);
        if(save){
            return ActionResult.success("修改成功");
        }else{
            return ActionResult.fail("修改失败");
        }
    }


}
